package com.web.controller;

import com.web.bean.选课及成绩;
import com.web.dao.选课及成绩Dao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

//定义当前请求处理的访问uri
@WebServlet("/insertAchievement")
public class AchievementInsertController extends HttpServlet {

    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        req.setCharacterEncoding("UTF-8");

        String 学号 = req.getParameter("学号");
        String 课程代号 = req.getParameter("课程代号");
        String 成绩 = req.getParameter("成绩");
        float s成绩 = Integer.parseInt(成绩);
        String 修课时间 = req.getParameter("修课时间");


        选课及成绩 acvt = new 选课及成绩(学号, 课程代号,s成绩,修课时间, null);
        选课及成绩Dao dao = new 选课及成绩Dao();
        boolean isok = false;
        try {
            isok = dao.insertAchievement(acvt);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        if (isok) {
            req.getRequestDispatcher("/getAllAchievement").forward(req, resp);
        } else {

            req.setAttribute("message", "添加操作失败，请重新操作");
            req.setAttribute("acvt", acvt);//保存对象数据，在页面表单中回显
            req.getRequestDispatcher("/AchievementInsert.jsp").forward(req, resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}